IF OBJECT_ID('ssf.ptx_insert_task_group') IS NOT NULL
	DROP PROCEDURE ssf.ptx_insert_task_group
GO

CREATE PROCEDURE ssf.ptx_insert_task_group
	@group_type NVARCHAR(128) = N'DEFAULT'
	, @group_name NVARCHAR(128)
	, @group_description NVARCHAR(2048) = NULL
	, @task_group_id INT = NULL OUTPUT
AS
/*
<object_header>

<object_summary>
	<purpose><![CDATA[

	]]></purpose>
</object_summary>

</object_header>
*/

SET NOCOUNT ON

-- ============
/*<code_comment><![CDATA[declare dynamic sql variables]]></code_comment>*/
DECLARE	@proc_error INT
			, @sql_error INT
			, @error_message NVARCHAR(2048)
			, @error_line INT
			, @row_count BIGINT
			, @nsql NVARCHAR(MAX)
			, @crlf CHAR(2) = CHAR(13) + CHAR(10)
			, @quote CHAR(1) = CHAR(39)
			, @tab CHAR(1) = CHAR(9)
-- ============

-- ============
/*<code_comment><![CDATA[check parameters]]></code_comment>*/
IF NULLIF(@group_type, N'') IS NULL
OR NULLIF(@group_name, N'') IS NULL
BEGIN
	RAISERROR('A required parameter is blank or null.', 16, 1)
	RETURN 1
END
-- ============

-- ============
/*<code_comment><![CDATA[insert task group]]></code_comment>*/
SET @task_group_id = NULL

INSERT ssf.ptx_task_group (
	group_type
	, group_name
	, group_description
)
VALUES (
	@group_type
	, @group_name
	, NULLIF(@group_description, N'')
)

SELECT @sql_error = @@ERROR
	, @row_count = @@ROWCOUNT
	, @task_group_id = SCOPE_IDENTITY()

IF @sql_error <> 0
BEGIN
	RAISERROR('The INSERT into the table <ssf.ptx_task_group> failed.', 16, 1)
	RETURN @sql_error
END
-- ============

RETURN @@ERROR
GO
